/**
 * 
 */
package com.iptv.interact;

import java.util.List;

import com.base.Right;
import com.iptv.IptvUser;
import com.iptv.LoginUserManager;
import com.iptv.TreeNodeFormatRight;
import com.iptv.interact.request.AddProgramRequest;
import com.iptv.interact.request.AddUserRequest;
import com.iptv.interact.request.GetProgramListRequest;
import com.iptv.interact.request.LoginResquest;
import com.iptv.interact.response.AddProgramResponse;
import com.iptv.interact.response.AddUserResponse;
import com.iptv.interact.response.GetProgramListResponse;
import com.iptv.interact.response.LoginResponse;
import com.iptv.target.Program;

/**
 * @author 4833 robben
 * 2014-5-11 上午12:17:06
 * mock interface server
 */
public class InterfaceServer {
	
	public LoginResponse login(LoginResquest request) {
		LoginResponse response = new LoginResponse();
		
		IptvUser iu = new IptvUser();
		iu.setName(request.getUsername());
		iu.setPassword(request.getPassword());
		iu = iu.login();
		
		// TODO: transform list right to tree-node right format.
		List<Right> rights = iu.getRights();
		TreeNodeFormatRight treeNodeRight = new TreeNodeFormatRight();
		
		response.setCode(100);
		response.setDescription("logon success !");
		response.setUser(iu);
		response.setTreeNodeRight(treeNodeRight);
		
		return response;
	}
	
	public AddUserResponse addUser(AddUserRequest request) {
		AddUserResponse response = new AddUserResponse();
		
//		if (!LoginUserManager.isOnline(request.getOperator().getCode())) {
		if (false) {
			response.setCode(199);
			response.setDescription("you need to login first !");
		} else {
			IptvUser operator = LoginUserManager.find(request.getOperator().getCode());
			IptvUser newUser = new IptvUser();
			newUser.setName(request.getNewUser().getName());
			newUser.setPassword(request.getNewUser().getPassword());
			
			String newUserCode = operator.addIptvUser(newUser);
			response.setCode(100);
			response.setDescription("add new user success");
			response.setNewUserCode(newUserCode);
		}
		
		return response;
	}
	
	public AddProgramResponse addProgram(AddProgramRequest request) {
		AddProgramResponse response = new AddProgramResponse();
		
//		if (!LoginUserManager.isOnline(request.getOperator().getCode())) {
		if (false) {
			response.setCode(199);
			response.setDescription("you need to login first !");
		} else {
			IptvUser operator = LoginUserManager.find(request.getOperator().getCode());
			
			Program p = new Program();
			p.setName(request.getName());
			
			String pCode = operator.addProgram(p);
			response.setCode(100);
			response.setNewProgramCode(pCode);
			response.setDescription("add program success");
		}
		
		return response;
	}
	
	public GetProgramListResponse getProgramList(GetProgramListRequest request) {
		GetProgramListResponse response = new GetProgramListResponse();
		
//		if (!LoginUserManager.isOnline(request.getOperator().getCode())) {
		if (false) {
			response.setCode(199);
			response.setDescription("you need to login first !");
		} else {
			IptvUser operator = LoginUserManager.find(request.getOperator().getCode());
			
			List<Program> programList = operator.getProgramList();
			response.setCode(100);
			response.setDescription("add program success");
		}
		
		return response;
	}
	
}
